package com.sundi.ecsystem.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.eccommon.dto.EcUserDto;
import com.sundi.eccommon.vo.ResultEntity;
import com.sundi.ecsystem.entity.EcUser;
import com.sundi.ecsystem.service.IEcUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/ecUser")
@Slf4j
public class EcUserController {

    @Autowired
    IEcUserService userService;

    @RequestMapping("userList")
    public ResultEntity list(EcUserDto ecUserDto){
        PageHelper.startPage(ecUserDto.getPageNum(),ecUserDto.getPageSize());

        Wrapper<EcUser> wrapper = new EntityWrapper<EcUser>();
        wrapper.eq("deleted",0);
        if (ecUserDto.getUserName()!=null){
            wrapper.eq("user_name",ecUserDto.getUserName());
        }
        List<EcUser> ecUsers = userService.selectList(wrapper);
        PageInfo<EcUser> pageInfo  = new PageInfo<>(ecUsers);
        return ResultEntity.ok(pageInfo);
    }

    //导出Excel
    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(EcUserDto ecUserDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = userService.downLoadGoodsExcel(ecUserDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("用户表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }
    //导入Excel
    @RequestMapping("/upLoadExcel")
    public ResultEntity  upLoadExcel(MultipartFile file) throws Exception {
        if (file == null){
            throw new Exception("文件为空");
        }
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        try {
            this.userService.uplodExcel(file);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new Exception("导入数据失败");
        }
        return ResultEntity.ok("导入数据成功");
    }


    @RequestMapping("addUser")
    public ResultEntity add(@RequestBody EcUserDto ecUserDto){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_name",ecUserDto.getUserName());
        EcUser user = userService.selectOne(wrapper);
        if (user!=null){
            return ResultEntity.error("您设置的用户名已存在");
        }
        EcUser ecUser = new EcUser();
        BeanUtils.copyProperties(ecUserDto,ecUser);
        ecUser.setPassword("123");
        ecUser.setDeleted(1);
        userService.insert(ecUser);
        return ResultEntity.ok("添加成功");
    }

    @RequestMapping("updUser")
    public ResultEntity upd(@RequestBody EcUserDto ecUserDto){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_name",ecUserDto.getUserName());
        EcUser user = userService.selectOne(wrapper);
        if (user!=null){
            return ResultEntity.error("您更改的用户名已存在");
        }
        EcUser ecUser = new EcUser();
        BeanUtils.copyProperties(ecUserDto,ecUser);
        userService.updateById(ecUser);
        return ResultEntity.ok("修改成功");
    }

    @RequestMapping("delUser")
    public ResultEntity del(@RequestBody EcUserDto ecUserDto){
        Wrapper<EcUser> wrapper = new EntityWrapper<EcUser>();
        wrapper.eq("id",ecUserDto.getId());
        EcUser user = new EcUser();
        BeanUtils.copyProperties(ecUserDto,user);
        user.setDeleted(1);
        userService.update(user,wrapper);
        return ResultEntity.ok("逻辑删除成功");
    }


}
